#include "SRRenderAppGLES1.h"
#include "SRGLMachine.h"
#include "SRModelLoader.h"
#include "SRPrimitiveModel.h"
#include "SRGLinc.h"
#include "SRMacro.h"

#if _GLES1_
namespace sora
{
	RenderAppGLES1::RenderAppGLES1()
	: RenderApp()
	{
		init();
	}
	RenderAppGLES1::~RenderAppGLES1()
	{
		deinit();
	}
	void RenderAppGLES1::init()
	{
		/*
		//load image
		tex_ = TextureManager::mgr().loadOGLTexture(
													"img_test.tga",
													SOIL_LOAD_AUTO,
													SOIL_CREATE_NEW_ID,
													SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
													);
		*/
		//load model
		//model_ = ModelLoader::loader().load("asset/model/obj/cube.obj");
		//model_ = ModelLoader::loader().loadWireframe(model_);
		//model_ = std::auto_ptr<Model>(loader.load("asset/model/obj/cube.obj"));
		//model_ = std::auto_ptr<Model>(loader.load("asset/model/obj/cutcube.obj"));
		//model_ = std::auto_ptr<Model>(loader.load("asset/model/obj/pawn.obj"));
		//model_ = std::auto_ptr<Model>(loader.load("asset/model/obj/dragon.obj"));
		//model_ = std::auto_ptr<Model>(loader.load("asset/model/obj/teapot.obj"));
		//model_ = std::auto_ptr<Model>(loader.load("asset/model/obj/womanhead.obj"));
		//model_ = std::auto_ptr<Model>(loader.load("asset/model/obj/head_chord.obj"));


		//MeshModel *m = static_cast<MeshModel*>(loader.load("asset/model/obj/pawn.obj"));
		//Model *wireframe = new WireFrameModel(*m);
		//model_ = std::auto_ptr<Model>(wireframe);
	}
	void RenderAppGLES1::deinit()
	{
	}
	void RenderAppGLES1::render(long tick, int width, int height)
	{
		SR_ASSERT(width > 0 && height > 0);

		glEnable(GL_TEXTURE_2D);
		glEnable(GL_CULL_FACE);

		glEnable( GL_DEPTH_TEST );
		glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations

		glViewport(0, 0, width, height);
		glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		//TODO: 1.1용 카메라 구현하기
		float aspect = (float)width / (float)height;
		//gles1::gluPerspective(45, aspect, 0, 10);
		gles1::gluPerspective( 45.0f, aspect, 0.1f, 100.0f );

		//set color

		{
			GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f }; 				// Ambient Light Values ( NEW )
			GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };				 // Diffuse Light Values ( NEW )
			GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };				 // Light Position ( NEW )

			glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);				// Setup The Ambient Light
			glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);				// Setup The Diffuse Light
			glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);			// Position The Light
			glEnable(GL_LIGHT1);							// Enable Light One
			glEnable(GL_LIGHTING);
		}

		/*
		 //blend
		 {
		 glColor4f(1.0f,1.0f,1.0f,0.5f);			// Full Brightness, 50% Alpha ( NEW )
		 glBlendFunc(GL_SRC_ALPHA,GL_ONE);		// Blending Function For Translucency Based On Source Alpha Value ( NEW )
		 glEnable(GL_BLEND);		// Turn Blending On
		 glDisable(GL_DEPTH_TEST);	// Turn Depth Testing Off
		 glDisable(GL_CULL_FACE);	//blend모드에서 뒷면안그리기가 되면 큐브의 비치는 면을 그리지 않아버린다
		 }
		 */

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

		static GLfloat xrot;
		static GLfloat yrot;
		static GLfloat zrot;

		glTranslatef(0.0f,0.0f,-5.0f);

		glColor4f(1, 1, 1, 1);

		glPushMatrix();
		{
			glTranslatef(-1.5, 0, 0);

			glRotatef(xrot,1.0f,0.0f,0.0f);
			glRotatef(yrot,0.0f,1.0f,0.0f);
			glRotatef(zrot,0.0f,0.0f,1.0f);

			//glBindTexture(GL_TEXTURE_2D, tex_->texId());
			//gles1::glutSolidCube(2);
			//gles1::glutWireCube(2);
			//gles1::glutWireSphere(1, 8, 8);
			//gles1::glutSolidSphere(1, 16, 16);
			//gles1::glutWireCone(1, 2, 8, 8);
			//gles1::glutSolidCone(0.5, 2, 8, 8);
			//glScalef(3, 3, 3);
			//glScalef(0.002, 0.002, 0.002);	//pawn
			//glScalef(0.5, 0.5, 0.5);
			//model_ = ModelLoader::loader().load("asset/model/obj/cutcube.obj");
			//model_ = ModelLoader::loader().load("asset/model/obj/head_chord.obj");

			gles1::drawModel(model_);

			//glScalef(1.01, 1.01, 1.01);
			//model_ = ModelLoader::loader().loadWireframe(model_);
			//gles1::drawModel(model_);

			//const sora::BoundingBox &box = model_->boundingBox();
			//sora::gles1::drawModel(&box);
		}
		glPopMatrix();



		glTranslatef(1.5, 0, 0);

		glRotatef(xrot,1.0f,0.0f,0.0f);
		glRotatef(yrot,0.0f,1.0f,0.0f);
		glRotatef(zrot,0.0f,0.0f,1.0f);

		//glBindTexture(GL_TEXTURE_2D, tex_->texId());
		//gles1::glutSolidSphere(1, 16, 16);
		//get color
		GLfloat color[4];
		glGetFloatv(GL_CURRENT_COLOR, color);
		matsu::vec4 colorVec(color);
		sora::WireSphere sphere(1, 16, 16, colorVec);
		sora::gles1::drawModel(&sphere);

		//const sora::BoundingBox &box = sphere.boundingBox();
		//sora::gles1::drawModel(&box);

		xrot+=0.3f;
		yrot+=0.2f;
		zrot+=0.4f;

	}
}

#endif